package com.ruoyi.basic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.basic.domain.Column;
import com.ruoyi.basic.service.IColumnService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 栏目管理Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/basic/column")
public class ColumnController extends BaseController
{
    @Autowired
    private IColumnService columnService;

    /**
     * 查询栏目列表
     */
    @PreAuthorize("@ss.hasPermi('basic:column:list')")
    @GetMapping("/list")
    public AjaxResult list(Column column)
    {
        List<Column> list = columnService.selectColumnList(column);
        return success(list);
    }

    /**
     * 查询栏目列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('basic:column:list')")
    @GetMapping("/list/exclude/{columnId}")
    public AjaxResult excludeChild(@PathVariable(value = "columnId", required = false) Long columnId)
    {
        List<Column> columns = columnService.selectColumnList(new Column());
        columns.removeIf(d -> d.getColumnId().intValue() == columnId.intValue()
                || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), columnId + ""));
        return success(columns);
    }

    /**
     * 获取栏目详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:column:query')")
    @GetMapping(value = "/{columnId}")
    public AjaxResult getInfo(@PathVariable("columnId") Long columnId)
    {
        return AjaxResult.success(columnService.selectColumnById(columnId));
    }

    /**
     * 新增栏目
     */
    @PreAuthorize("@ss.hasPermi('basic:column:add')")
    @Log(title = "栏目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Column column)
    {
        column.setCreateBy(getUsername());
        return toAjax(columnService.insertColumn(column));
    }

    /**
     * 修改栏目
     */
    @PreAuthorize("@ss.hasPermi('basic:column:edit')")
    @Log(title = "栏目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Column column)
    {
        if (column.getParentId() != null && column.getParentId().equals(column.getColumnId()))
        {
            return error("修改栏目'" + column.getName() + "'失败，上级栏目不能是自己");
        }
        column.setUpdateBy(getUsername());
        return toAjax(columnService.updateColumn(column));
    }

    /**
     * 删除栏目
     */
    @PreAuthorize("@ss.hasPermi('basic:column:remove')")
    @Log(title = "栏目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{columnIds}")
    public AjaxResult remove(@PathVariable Long[] columnIds)
    {
        return toAjax(columnService.deleteColumnByIds(columnIds));
    }

    /**
     * 导出栏目列表
     */
    @PreAuthorize("@ss.hasPermi('basic:column:export')")
    @Log(title = "栏目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Column column)
    {
        List<Column> list = columnService.selectColumnList(column);
        ExcelUtil<Column> util = new ExcelUtil<Column>(Column.class);
        util.exportExcel(response, list, "栏目数据");
    }

    /**
     * 获取栏目树形结构
     */
    @PreAuthorize("@ss.hasPermi('basic:column:list')")
    @GetMapping("/tree")
    public AjaxResult getColumnTree(Column column)
    {
        List<Column> columns = columnService.selectColumnList(column);
        return success(columnService.buildColumnTree(columns));
    }

    /**
     * 获取栏目下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(Column column)
    {
        List<Column> columns = columnService.selectColumnList(column);
        return success(columnService.buildColumnTreeSelect(columns));
    }

    /**
     * 加载对应角色栏目列表树（已弃用，保留兼容）
     */
    @GetMapping(value = "/columnTreeselect/{roleId}")
    public AjaxResult columnTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<TreeSelect> tree = columnService.selectColumnTreeList(new Column());
        return success(tree);
    }

    /**
     * 修改栏目状态
     */
    @PreAuthorize("@ss.hasPermi('basic:column:edit')")
    @Log(title = "栏目状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Column column)
    {
        return toAjax(columnService.changeStatus(column));
    }
}